﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using dhDataAcess.baseBLL;
using dhDataAcess.orderLinqDAL;
using dhDataAcess.dataAcess.orderDataSetTableAdapters;
using dhDataAcess.dataAcess;
using System.Data.SqlClient;
using dhDataAcess.orderDAL;

namespace dhSystemBLL.orderData
{
   public  class productTypeBLL
    {
       private productTypeTableAdapter _productTypeAdapter = null;
       private productTypeTableAdapter productTypeAdapter
        {
            get
            {
                if (_productTypeAdapter == null) _productTypeAdapter = new productTypeTableAdapter();
                return _productTypeAdapter;
            }
        }
        /// <summary>
        /// 查询产品类型表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public orderDataSet.productTypeDataTable getproductTypeAll()
        {
            try
            {
                return productTypeAdapter.GetDataproductTypeAll();
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 根据类别名称查询产品类型表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public orderDataSet.productTypeDataTable getproductTypeAll(string pType)
        {
            try
            {
                return productTypeAdapter.GetDataproductTypeBypType(pType);
            }
            catch (Exception ex)
            {
                throw new Exception(ex.Message.ToString());
            }
        }
        /// <summary>
        /// 插入产品类型表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int productTypeInsert(productTypeModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productTypeDAL productType = new productTypeDAL(tn, conn);
                productType.productTypeInsert(row);
                tn.Commit();
                return i;

            }
            catch (SqlException ex)
            {
                tn.Rollback();
                if (ex.Number == 2627)
                {
                    throw new Exception("这个类别名称已经存在");
                }


                throw new Exception(ex.Number.ToString() + ex.Message.ToString());

            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
               
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
        /// 更新产品类型表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int productTypeUpdate(productTypeModel row)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productTypeDAL productType = new productTypeDAL(tn, conn);
                productType.productTypeUpdate(row);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
        /// <summary>
       /// 删除一行产品类型表
        /// </summary>
        /// <param name="row"></param>
        /// <returns></returns>
       public int productTypeDel(string pType)
        {
            int i = 0;
            SqlConnection conn = dataConn.connOpen();
            SqlTransaction tn = null;
            try
            {
                tn = conn.BeginTransaction();
                productTypeDAL productType = new productTypeDAL(tn, conn);
                productType.productTypeDel(pType);
                tn.Commit();
                return i;
            }
            catch (Exception ex)
            {
                tn.Rollback();
                throw new Exception(ex.Message.ToString());
                
            }
            finally
            {
                conn.Close();
            }
        }
    }
}
